package uk.petesoft.remindme;

import java.awt.*;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.JSpinner.NumberEditor;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

public class appGUI {

	private JFrame frmRemindMe;
	private JPanel pnlClock;
	private JPanel pnlTimeControls;
	private JClockFace clkClockFace;
	private JSpinner spnHours;
	private JLabel lblColon;
	private JSpinner spnMinutes;
	private JPanel pnlRadio;
	private JRadioButton rdbtnPM;
	private JRadioButton rdbtnAM;
	private JPanel pnlButtons;
	private JButton btnSetAlarm;
	private final ButtonGroup btngrpAMPM = new ButtonGroup();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					appGUI window = new appGUI();
					window.frmRemindMe.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public appGUI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmRemindMe = new JFrame();
		frmRemindMe.setTitle("Remind Me");
		frmRemindMe.setBounds(100, 100, 400, 400);
		frmRemindMe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		pnlClock = new JPanel();
		frmRemindMe.getContentPane().add(pnlClock, BorderLayout.CENTER);
			
		pnlTimeControls = new JPanel();
		frmRemindMe.getContentPane().add(pnlTimeControls, BorderLayout.NORTH);
				
		spnHours = new JSpinner();
		spnHours.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				clkClockFace.setHourHand((Integer)spnHours.getValue());
			}
		});
		spnHours.setModel(new SpinnerCircularNumberModel(12, 1, 12, 1));
		
		//format the hour spinner to always show two digits
		NumberEditor nedHours = new JSpinner.NumberEditor(spnHours);
		nedHours.getFormat().setMinimumIntegerDigits(2);
		nedHours.getFormat().setMaximumIntegerDigits(2);
		spnHours.setEditor(nedHours);
		pnlTimeControls.add(spnHours);
		
		lblColon = new JLabel(":");
		pnlTimeControls.add(lblColon);
		
		spnMinutes = new JSpinner();
		spnMinutes.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				clkClockFace.setMinuteHand((Integer)spnMinutes.getValue());
			}
		});
		spnMinutes.setModel(new SpinnerCircularNumberModel(0, 0, 59, 1));
		
		//format the minutes spinner to always show two digits
		NumberEditor nedMinutes = new JSpinner.NumberEditor(spnMinutes, "00");
		nedMinutes.getFormat().setMinimumIntegerDigits(2);
		nedMinutes.getFormat().setMaximumIntegerDigits(2);
		spnMinutes.setEditor(nedMinutes);
		pnlTimeControls.add(spnMinutes);
		
		pnlRadio = new JPanel();
		pnlTimeControls.add(pnlRadio);
		pnlRadio.setLayout(new BoxLayout(pnlRadio, BoxLayout.Y_AXIS));
		
		rdbtnAM = new JRadioButton("AM");
		btngrpAMPM.add(rdbtnAM);
		btngrpAMPM.setSelected(rdbtnAM.getModel(), true);
		pnlRadio.add(rdbtnAM);
		
		rdbtnPM = new JRadioButton("PM");
		btngrpAMPM.add(rdbtnPM);
		pnlRadio.add(rdbtnPM);
		
		pnlButtons = new JPanel();
		frmRemindMe.getContentPane().add(pnlButtons, BorderLayout.SOUTH);
		pnlButtons.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
		
		btnSetAlarm = new JButton("Set Alarm");
		
		//SetAlarm button event listener, prints message into dialog
		btnSetAlarm.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				String alarm = "Alarm set for " + String.format("%02d", spnHours.getValue()) +
						":" + 
							String.format("%02d", spnMinutes.getValue()) +
									" " + getMeridiem();
				JOptionPane.showMessageDialog(null, alarm);
			}
		});
		pnlButtons.add(btnSetAlarm);
		
		clkClockFace = new JClockFace("");
		
		//gets the image icon for the clockface widget
		try {
			BufferedImage imgClockFace = ImageIO.read(new File("resources/GUI/clock_face.png"));
			clkClockFace.setIcon(new ImageIcon(imgClockFace));
		} catch (IOException ex) {
			ex.printStackTrace();
		}		
		
		clkClockFace.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				if (arg0.getButton() == 1) {
					spnHours.setValue(clkClockFace.getSegment(arg0));
				} else if (arg0.getButton() == 3) {
					spnMinutes.setValue(clkClockFace.getSegment(arg0));
				}
			}
		});
		
		pnlClock.add(clkClockFace);
	}
	
	
	/**
	 * Return whether the user has selected AM or PM from the radio buttons
	 */
	private String getMeridiem() {
		String meridiem = "";
		
		if (btngrpAMPM.getSelection() == rdbtnAM.getModel()) {
			meridiem = "AM";
		} else if (btngrpAMPM.getSelection() == rdbtnPM.getModel()) {
			meridiem = "PM";
		}
		return meridiem;
	}
}
